﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace Dqaqs
{
	/// <summary>
	/// Represents an instance of a single selection condition
	/// </summary>
	public class SelectionCondition
	{
		private readonly Expression _expression;
		private readonly List<ParameterExpression> _parameters;
		public Expression Expression { get { return _expression; } }
		public string GetConditionOperand1()
		{
			var binExp = _expression as BinaryExpression;
			if (binExp == null)
				throw new InvalidOperationException(String.Format("Expression {0} is not a binary expression. Only binary expression is supported.", _expression));
			return _parameters.First().Name;
		}
		private SelectionCondition(IEnumerable<ParameterExpression> parameters, Expression expression)
		{
			_expression = expression;
			_parameters = new List<ParameterExpression>(parameters);
		}
		/// <summary>
		/// Creates a selection condition from a lambda expression
		/// </summary>
		/// <param name="expression">The expression.</param>
		/// <returns>The selection condition</returns>
		public static SelectionCondition Create<T>(Expression<Func<T,bool>> expression)
		{
			return new SelectionCondition(expression.Parameters, expression.Body);
		}
	}
}